package com.example.administrator.compileapp.activity;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.administrator.compileapp.R;
import com.example.administrator.compileapp.databinding.ActivityOtherStartBinding;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class OtherStartActivity extends AppCompatActivity {
   private ActivityOtherStartBinding binding;
    private String loadUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_other_start);
        binding.webViewOtherStart.setInitialScale(50);//这里一定要设置，数值可以根据各人的需求而定，我这里设置的是50%的缩放
        loadUrl = getIntent().getStringExtra("webUrl");
        WebSettings webSettings = binding.webViewOtherStart.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setBuiltInZoomControls(true);// support zoom
        webSettings.setUseWideViewPort(true);// 这个很关键
        webSettings.setLoadWithOverviewMode(true);
        init();
    }
    private void init() {
        binding.webViewOtherStart.loadUrl(loadUrl);
        binding.webViewOtherStart.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        binding.webViewOtherStart.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                if (newProgress == 100) {
                    binding.webviewFinish.setVisibility(View.GONE);
                } else {
                 binding.webviewFinish.setVisibility(View.VISIBLE);
                }
            }
        });
    }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode== android.view.KeyEvent.KEYCODE_BACK)
            {
                if(binding.webViewOtherStart.canGoBack())
                {
                    binding.webViewOtherStart.goBack();//返回上一页面
                    return true;
                }
                else
                {
                    finish();
                  //  System.exit(0);//退出程序
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    }
